package com.gogowan.agroenterprise.storage;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.contorl.MyAsyncTask;
import com.gogowan.agroenterprise.model.LoginResultInfo;
import com.gogowan.agroenterprise.model.ResultInfo;
import com.gogowan.agroenterprise.model.StoreModel;
import com.gogowan.agroenterprise.utils.Utils;

/**
 * 货位信息
 * 
 * @author tjh 2017-01-06
 *
 */
@SuppressLint("NewApi")
public class GoodsAllocationInfoActivity extends Activity implements
		View.OnClickListener {
	private ProgressDialog progressDialog;
	private StoreModel storeModel;
	private EditText hwmcEdit;
	private TextView hwmcText;
	private ImageView hwmtImage;
	private Button bjhwBtn;
	private boolean isEdit = false;
	private long pstoreID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_goods_allocation_info);

		initValue();
		initView();
	}

	/**
	 * 初始化数据
	 */
	private void initValue() {
		String storeModelJson = this.getIntent().getStringExtra(
				"storeModelJson");
		storeModel = (StoreModel) Utils.fromJSON(storeModelJson,
				StoreModel.class);
		pstoreID = this.getIntent().getLongExtra("pstoreID", 0);
	}

	/**
	 * 初始化界面
	 */
	private void initView() {
		WindowManager wm = this.getWindowManager();
		int width = wm.getDefaultDisplay().getWidth();

		hwmcEdit = (EditText) findViewById(R.id.goods_allocation_info_hwmc_edit);
		hwmcText = (TextView) findViewById(R.id.goods_allocation_info_hwmc_text);
		hwmtImage = (ImageView) findViewById(R.id.goods_allocation_info_hwmt_image);
		bjhwBtn = (Button) findViewById(R.id.goods_allocation_info_bjhw_btn);
		bjhwBtn.setOnClickListener(this);

		hwmcText.setText(storeModel.getStoreName());
		hwmcEdit.setText(storeModel.getStoreName());

		LayoutParams para = hwmtImage.getLayoutParams();
		para.height = (int) (width * 0.5);
		para.width = (int) (width * 0.5);
		hwmtImage.setLayoutParams(para);

		// 彩色二维码生成
		final Bitmap ewmBit = Utils.create2DCoderBitmap(storeModel.getEwmUrl(),
				width, width);
		hwmtImage.setImageBitmap(ewmBit);
		hwmtImage.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				String pathStr = Utils.saveEWMBitmapToFile(ewmBit,
						storeModel.getStoreName() + ".png");
				if (pathStr == "") {
					Toast.makeText(GoodsAllocationInfoActivity.this, "保存失败",
							200).show();
				} else {
					MediaScannerConnection
							.scanFile(
									GoodsAllocationInfoActivity.this,
									new String[] { pathStr },
									new String[] { "image/jpeg" },
									new MediaScannerConnection.OnScanCompletedListener() {
										@Override
										public void onScanCompleted(
												String path, Uri uri) {
										}
									});
					Toast.makeText(GoodsAllocationInfoActivity.this,
							"保存成功：" + pathStr, 200).show();
				}
				return false;
			}
		});

		ImageButton backBtn = (ImageButton) findViewById(R.id.goods_allocation_info_fh_btn);
		backBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.goods_allocation_info_fh_btn:
			// 返回
			this.finish();
			break;
		case R.id.goods_allocation_info_bjhw_btn:
			if (isEdit) {
				// 保存
				if (TextUtils.isEmpty(hwmcEdit.getText())) {
					Toast.makeText(this, "请输入货位名称", 200).show();
				} else {
					progressDialog = ProgressDialog.show(this,
							getString(R.string.alert_title),
							getString(R.string.alert_message), true, false);

					SharedPreferences preferences = getSharedPreferences(
							"LoginINfo", MODE_PRIVATE);
					
//					LoginResultInfo loginResultInfo = (LoginResultInfo) Utils
//							.fromJSON(string, LoginResultInfo.class);

					StoreModel StoreModel2 = new StoreModel();
//					StoreModel2.setEnterprise_Info_ID(loginResultInfo
//							.getEnterpriseinfo().getEnterpriseid());
//					StoreModel2.setDealerID(loginResultInfo.getEnterpriseinfo()
//							.getDealerId());
					StoreModel2.setStoreID(storeModel.getStoreID());
					StoreModel2.setStoreName(hwmcEdit.getText() + "");
					StoreModel2.setParentCode(pstoreID);
					StoreModel2.setType(2);
//					StoreModel2.setAddUser(loginResultInfo.getUserinfo()
//							.getPersonid());
//					StoreModel2.setUserType(loginResultInfo.getEnterpriseinfo()
//							.getUserType());

					SaveWarehouseInfoAsyn saveWarehouseInfoAsyn = new SaveWarehouseInfoAsyn();
					saveWarehouseInfoAsyn.execute(StoreModel2);
				}
			} else {
				// 编辑
				isEdit = true;
				bjhwBtn.setText("保存");
				hwmcText.setVisibility(View.GONE);
				hwmcEdit.setVisibility(View.VISIBLE);
				hwmcEdit.setSelectAllOnFocus(true);
				hwmcEdit.requestFocus();

				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.showSoftInput(hwmcEdit, InputMethodManager.SHOW_FORCED);
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 保存编辑仓库信息
	 * 
	 * @author tjh
	 *
	 */
	private class SaveWarehouseInfoAsyn extends MyAsyncTask {

		@Override
		protected void onPostExecute(Object res) {
			if (res == null || res.equals("")) {
				Toast.makeText(GoodsAllocationInfoActivity.this,
						getString(R.string.server_error), Toast.LENGTH_LONG)
						.show();
			} else {
				com.gogowan.agroenterprise.model.Message message = (com.gogowan.agroenterprise.model.Message) Utils
						.fromJSON((String) res,
								com.gogowan.agroenterprise.model.Message.class);
				String string = Utils.getMessageContent(message);

				if (string == null || string.equals("")) {
					Toast.makeText(GoodsAllocationInfoActivity.this,
							getString(R.string.analyse_error),
							Toast.LENGTH_LONG).show();
				} else {
					ResultInfo resultInfo = (ResultInfo) Utils.fromJSON(string,
							ResultInfo.class);

					if (resultInfo.getResult() == 21200) {
						Intent intent = new Intent(
								GoodsAllocationInfoActivity.this,
								WarehouseInfoActivity.class);
						setResult(RESULT_OK, intent);
						GoodsAllocationInfoActivity.this.finish();
					}
					Toast.makeText(GoodsAllocationInfoActivity.this,
							resultInfo.getResultinfo(), Toast.LENGTH_LONG)
							.show();
				}
			}
			progressDialog.dismiss();
			super.onPostExecute(res);
		}

		@Override
		protected Object doInBackground(Object... params) {
			StoreModel storeModel = (StoreModel) params[0];
			String json = Utils.toJSON(storeModel);
			com.gogowan.agroenterprise.model.Message message = new com.gogowan.agroenterprise.model.Message(
					21200, true, json);
			// String ss = Utils.toJSON(message);
			// Log.i("gggggggggggggggggg===", ss);
			String res = Utils.postCommit(getString(R.string.first_url)
					+ getString(R.string.first_url_page), message);
			return res;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
